﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_DL_Project_ManageRegion : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOCity objCity = new TOCity();
    VOTOCity voCity = new VOTOCity();
    TORegion objRegion = new TORegion();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int CID = 0;

            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                voCity = objCity.Load(CID);
            }
            ViewState["CID"] = CID.ToString();
            voCity = objCity.Load(CID);
            if (voCity != null)
            {
                BinderProvince(voCity.ProID);
                BinderCity(voCity.ProID, CID);
            }
            else
            {
                BinderProvince(0);
                BinderCity(0, CID);
            }


            InitPage();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
            if (CheckUser())
            {
                btnAddNew.Enabled = true;
                btn_DelCheck.Enabled = true;
            }
            else
            {
                btnAddNew.Enabled = false;
                btn_DelCheck.Enabled = false;
            }
        }
    }
    private bool CheckUser()
    {
        bool rt = false;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            if (login.VOAdvancedUser == null)
            {
                rt = false;
            }
            else if (login.VOAdvancedUser.AUserGroup == 1)
            {
                rt = true;
            }
        }
        return rt;

    }


    private void BinderProvince(int id)
    {
        TOProvince objPro = new TOProvince();
        DataTable dt = new DataTable();

        dt = objPro.Query("");
        ddl_Province.DataSource = dt;
        ddl_Province.DataTextField = "Province";
        ddl_Province.DataValueField = "ID";
        ddl_Province.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_Province.Items.Insert(0, li);
        ddl_Province.SelectedValue = id.ToString();
    }

    private void BinderCity(int pid, int cid)
    {
        DataTable dt = new DataTable();
        if (pid > 0)
            dt = objCity.Query("ProID=" + pid.ToString());
        else
            dt = objCity.Query("");


        ddl_City.DataSource = dt;
        ddl_City.DataTextField = "City";
        ddl_City.DataValueField = "ID";
        ddl_City.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "0";
        ddl_City.Items.Insert(0, li);

        ddl_City.SelectedValue = cid.ToString();
    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerNew.Text = voCity.City + "&nbsp;&nbsp;区县列表";
        //lbl_ManagerNew.Text = "新闻列表";
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = " 1=1 ";
        if (ViewState["CID"].ToString() != "0")
            search =search+ "and CityID= " + ViewState["CID"].ToString();
        if (!string.IsNullOrEmpty(txtNewTitle.Text))
            search =search +string.Format(" and Region like '%{0}%' ", txtNewTitle.Text.Trim());
       // search = search + System.String.Format(" and Id in (Select FkOID from TauthorityUser as a , TauthorityList as b where a. FkAuthorityID = b.FkAuthorityID and FkUserID ={0} and b.authorityType=0)", GetLoginEntity.UserID);
        //如果是超级管理员登录可以查看所有课程
        if (GetLoginEntity.UserName.ToLower() != "admin" && GetLoginEntity.UserName.ToLower() != "administrator")
        {
            search = search + System.String.Format(" and (Id in (Select FkOID from TauthorityUser as a , TauthorityList as b where a. FkAuthorityID = b.FkAuthorityID and FkUserID ={0} and b.authorityType=0) or CreatorID ={0})", GetLoginEntity.UserID);

        }
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objRegion.Count(search);
        AspNetPager1.PageSize = objRegion.Count(search);
    }
    private void BinderPG(string search,int pageIndex)
    {
        string orderby = " ID desc";
        //Response.Write(search);
        //Response.End();
        DataTable dt = objRegion.Query(search, orderby, LMSPageSize, pageIndex);
        rptNew.DataSource = dt;
        rptNew.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            if (CheckUser())
            {
                lnkbtn_editor.Enabled = true;
                lbtn.Enabled = true;
                lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
            }
            else
            {
                lnkbtn_editor.Enabled = false;
                lbtn.Enabled = false;
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search=" 1=1 ";
        if(ddl_City.SelectedValue!="0")
            search =search+ " and CityID= " + ddl_City.SelectedValue.ToString();
        if (!string.IsNullOrEmpty(txtNewTitle.Text))
            search = search+string.Format(" and Region like '%{0}%' ", txtNewTitle.Text.Trim());
        //如果是超级管理员登录可以查看所有课程
        if (GetLoginEntity.UserName.ToLower() != "admin" && GetLoginEntity.UserName.ToLower() != "administrator")
        {
            search = search + System.String.Format(" and (Id in (Select FkOID from TauthorityUser as a , TauthorityList as b where a. FkAuthorityID = b.FkAuthorityID and FkUserID ={0} and b.authorityType=0) or CreatorID ={0})", GetLoginEntity.UserID);      

        }
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddRegion.aspx?CID=" + ViewState["CID"];
        Script.RedirectURL(url);

        //if (!string.IsNullOrEmpty(txtNewTitle.Text))
        //{
        //    VOTOProvince vo = new VOTOProvince();
        //    vo.Province = txtNewTitle.Text.Trim();
        //    vo.CityNum = 0;
        //    obj.Add(vo);

        //}
        //txtNewTitle.Text = "";
        //string search = GetSearch();
        //BinderPager(search);
        //BinderPG(search, AspNetPager1.CurrentPageIndex);

    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "leditor")
        {

            string url = "AddRegion.aspx?CID=" + ViewState["CID"] + "&RegID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
           
        }
        else if (e.CommandName.ToLower() == "ldel")
        {

            int RegID = int.Parse(e.CommandArgument.ToString());
            VOTORegion vo = new VOTORegion();
            vo = objRegion.Load(RegID);
            int cid = vo.CityID;
            if (objRegion.Del(RegID))
            {
                voCity = objCity.Load(cid);
                voCity.RegionNum = voCity.RegionNum - 1;
                objCity.Update(voCity);


                string search = GetSearch();
                BinderPager(search);
                BinderPG(search, AspNetPager1.CurrentPageIndex);
            }
            else
            {
                lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
           
        }
        
       
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptNew.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        VOTORegion vo = new VOTORegion();
        try
        {
            for (int i = 0; i < rptNew.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
                Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
                if (cbox.Checked)
                {
                    int RegID = int.Parse(lab_NewID.Text);
                    vo = objRegion.Load(RegID);
                    int cid = vo.CityID;

                    if (objRegion.Del(RegID))
                    {
                        voCity = objCity.Load(cid);
                        voCity.RegionNum = voCity.RegionNum - 1;
                        objCity.Update(voCity);

                    }
                    else
                    {
                        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
                    }

                }
            }
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, AspNetPager1.CurrentPageIndex);
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }

    protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderCity(int.Parse(ddl_Province.SelectedValue), 0);
    }
}

